package org.spring.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.spring.websocket.MessageHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.socket.TextMessage;

@Controller
public class MessageController {
	
	@Autowired
	private MessageHandler handler;

	@RequestMapping(value="/chat",method=RequestMethod.GET)
	@ResponseBody
	public String send(String msg,HttpSession session) throws Exception{
		String username  = (String) session.getAttribute("username");
		handler.sendMsgToUsers(new TextMessage(new Date().toLocaleString()+" "+username+" said "+"<Br>"+msg));
		return "success";
	}
}
